//
//  MyAcceptMovieListVC.m
//  MovieDate
//
//  Created by 杨运涛 on 16/3/27.
//  Copyright © 2016年 MovieDate. All rights reserved.
//

#import "MyAcceptMovieListVC.h"
#import "AttentionDetailVC.h"

@interface MyAcceptMovieListVC () <UIAlertViewDelegate> {
    int pIntPage;
    
    NSString *pStrAgreeId;
    UIButton *pBtnSelect;
}

@property(nonatomic, strong) NSMutableArray *pDataDicArr;

@property (weak, nonatomic) IBOutlet UILabel *lblMessage;

@end

@implementation MyAcceptMovieListVC

- (void)initInfo
{
    [super initInfo];
    pIntPage = 1;
    _pDataDicArr = [NSMutableArray array];
    
    self.automaticallyAdjustsScrollViewInsets = NO;
    [self selAddLeftBackButton];
    [self selDefaultNavigationBackground];
    [self selDefaultNavigationWithTitle:@"约人看电影"];
    
    [self.pTableView setTableFooterView:[UIView new]];
    
    __weak typeof(self) weakSelf = self;
    self.pTableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
 
        pIntPage = 1;
        [weakSelf requestDataDicDown];
    }];
    
    self.pTableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        [weakSelf requestDataDic];
    }];
    
    [self.pTableView.mj_header beginRefreshing];
    [self.pTableView.mj_footer setHidden:YES];
}


#pragma mark - UITableViewDataSource, UITableViewDelegate

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return _pDataDicArr.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"idfMyAcceptMovieListCell"];
    NSDictionary *dict = [_pDataDicArr objectAtIndex:indexPath.section];
    
    UIImageView *imgHead = (UIImageView *)[cell.contentView viewWithTag:1301];
    UIImageView *imgSex = (UIImageView *)[cell.contentView viewWithTag:1302];
    UILabel *lblName = (UILabel *)[cell.contentView viewWithTag:1101];
    UILabel *lblAge = (UILabel *)[cell.contentView viewWithTag:1102];
    UILabel *lblTime = (UILabel *)[cell.contentView viewWithTag:1103];
    UILabel *lblAddress = (UILabel *)[cell.contentView viewWithTag:1104];

    [imgHead sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@", IP, [dict objectForKey:@"headImage"]]]];
    [imgHead.layer setCornerRadius:imgHead.frame.size.width / 2];
    [imgHead.layer setMasksToBounds:YES];
    [lblName setText:[dict objectForKey:@"nickname"]];
    [lblTime setText:[dict objectForKey:@"acceptTime"]];
    [lblAddress setText:[dict objectForKey:@"acceptAddress"]];
    [lblAge setText:[dict objectForKey:@"userAge"]];

    if ([[dict objectForKey:@"userSex"] isEqualToString:@"1"]) {
        [imgSex setImage:[UIImage imageNamed:@"sex_boy"]];
    } else {
        [imgSex setImage:[UIImage imageNamed:@"sex_girl"]];
    }
    UIButton *btnAgree = (UIButton *)[cell.contentView viewWithTag:1401];
    [btnAgree setAccessibilityIdentifier:[dict objectForKey:@"accepterId"]];
    if ([[dict objectForKey:@"acAgree"] isEqualToString:@"1"]) {
        [btnAgree setImage:[UIImage imageNamed:@"icon_cancel"] forState:UIControlStateNormal];
    } else {
        [btnAgree setImage:[UIImage imageNamed:@"icon_agree"] forState:UIControlStateNormal];
        [btnAgree addTarget:self action:@selector(btnAgreeAction:) forControlEvents:UIControlEventTouchUpInside];
    }
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 120;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 5;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 0.1;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    NSDictionary *dict = [_pDataDicArr objectAtIndex:indexPath.section];
    [self performSegueWithIdentifier:@"idfAttentionDetailVC4" sender:[dict objectForKey:@"accepterId"]];
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}
- (void)requestDataDicDown
{
    __weak typeof(self) weakSelf = self;
    
    [TDNetwork requestMyConfirmMovieList:MY_Accept_MovieList page:pIntPage pageSize:10 appId:_pModelMyPublish.appId releaseTime:_pModelMyPublish.releaseTime complete:^(BOOL succeed, NSString *ret, NSDictionary *dictionary) {
               [weakSelf.pDataDicArr removeAllObjects];
        if (succeed) {
            
            NSDictionary *dict = [dictionary valueForKey:@"res"];
            if (!dict || [dict allKeys].count == 0) {
                return;
            }
            NSArray *arr = [dict valueForKey:@"movieList"];
            NSLog(@"附近用户信息： %@", dictionary);
            [_pDataDicArr addObjectsFromArray:arr];
            
            [weakSelf.pTableView reloadData];
            
            if (arr.count == 10) {
                pIntPage ++;
                [self.pTableView.mj_footer setHidden:NO];
            } else {
                [self.pTableView.mj_footer setHidden:YES];
            }
        } else {
        }
        
        if (_pDataDicArr.count == 0) {
            [_lblMessage setHidden:NO];
        } else {
            [_lblMessage setHidden:YES];
        }

        [self.pTableView.mj_footer endRefreshing];
        [self.pTableView.mj_header endRefreshing];
    }];
}

- (void)requestDataDic
{
    __weak typeof(self) weakSelf = self;

    [TDNetwork requestMyConfirmMovieList:MY_Accept_MovieList page:pIntPage pageSize:10 appId:_pModelMyPublish.appId releaseTime:_pModelMyPublish.releaseTime complete:^(BOOL succeed, NSString *ret, NSDictionary *dictionary) {
        if (succeed) {

            NSDictionary *dict = [dictionary valueForKey:@"res"];
            if (!dict || [dict allKeys].count == 0) {
                return;
            }
            NSArray *arr = [dict valueForKey:@"movieList"];
            NSLog(@"附近用户信息： %@", dictionary);
            [_pDataDicArr addObjectsFromArray:arr];
            
            [weakSelf.pTableView reloadData];
            [self.pTableView.mj_footer endRefreshing];
            [self.pTableView.mj_header endRefreshing];
            
            if (arr.count == 10) {
                pIntPage ++;
                [self.pTableView.mj_footer setHidden:NO];
            } else {
                [self.pTableView.mj_footer setHidden:YES];
            }
        }
        
        if (_pDataDicArr.count == 0) {
            [_lblMessage setHidden:NO];
        } else {
            [_lblMessage setHidden:YES];
        }

    }];
}

- (void)btnAgreeAction:(UIButton *)sender
{
    pBtnSelect = sender;
    pStrAgreeId = sender.accessibilityIdentifier;

    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"你将要同意此人的应约要求?" delegate:self cancelButtonTitle:@"再想想" otherButtonTitles:@"同意", nil];
    [alert show];
    
    
}

#pragma mark - UIAlertViewDelegate

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex == 1) {
        [SVProgressHUD show];
        [TDNetwork requestConfirm:confirmSure appId:_pModelMyPublish.appId releaseTime:_pModelMyPublish.releaseTime acceptUid:[pStrAgreeId integerValue] complete:^(BOOL succeed, NSString *ret, NSDictionary *dictionary) {
            if (succeed) {
                [SVProgressHUD showSuccessWithStatus:@"发送成功"];

                [pBtnSelect setImage:[UIImage imageNamed:@"icon_cancel"] forState:UIControlStateNormal];
                [pBtnSelect setUserInteractionEnabled:NO];
                [[NSNotificationCenter defaultCenter] postNotificationName:NOTI_ADD_AGREE_MEET object:nil];
                [self.navigationController popViewControllerAnimated:YES];
                
                [self performSegueWithIdentifier:@"idfMyDateVC" sender:nil];
            } else {
                [SVProgressHUD dismiss];
            }
        }];
    }
}


////关注接口

-(void)attentionAction:(NSString *)uid{
    [SVProgressHUD show];
    [TDNetwork  requestCollection:MY_COLLECTION_List toUid: [_pModelMyPublish.uid integerValue] complete:^(BOOL succeed, NSString *ret, NSDictionary *dictionary) {
        if (succeed) {
            if ([ret isEqualToString:@"1"]) {
                [SVProgressHUD showSuccessWithStatus:@"关注成功"];
            }else{
                [SVProgressHUD showSuccessWithStatus:@"未知错误"];
            }
            
            
        }else{
            
            [SVProgressHUD showErrorWithStatus:@"网络请求失败"];
        }
    }];
    
}


#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    if ([segue.identifier isEqualToString:@"idfAttentionDetailVC4"]) {
        AttentionDetailVC *nextVC = segue.destinationViewController;
        nextVC.hiddenAttentionBtn = YES;
        nextVC.strID = sender;
    }
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}


@end
